package com.shangcheng.dubbo.api;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shangcheng.dubbo.mappers.SmsHomeAdvertiseMapper;
import com.shangcheng.model.domain.SmsHomeAdvertise;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsHomeAdvertiseApiImpl implements SmsHomeAdvertiseApi {

    @Autowired
    private SmsHomeAdvertiseMapper smsHomeAdvertiseMapper;

    /**
     * 添加广告
     *
     * @param smsHomeAdvertise
     */
    @Override
    public void save(SmsHomeAdvertise smsHomeAdvertise) {
        smsHomeAdvertiseMapper.insert(smsHomeAdvertise);
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public void delete(List<Integer> ids) {
        smsHomeAdvertiseMapper.deleteBatchIds(ids);
    }

    /**
     * 分页查询
     *
     * @param endTime
     * @param name
     * @param pageNum
     * @param pageSize
     * @param type
     * @return
     */
    @Override
    public IPage findList(String endTime, String name, Integer pageNum, Integer pageSize, Integer type) {
        //1.设置分页参数
        IPage<SmsHomeAdvertise> page = new Page<>(pageNum, pageSize);
        //2.设置查询条件
        LambdaQueryWrapper<SmsHomeAdvertise> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(endTime), SmsHomeAdvertise::getEndTime, endTime)
                .like(StrUtil.isNotBlank(name), SmsHomeAdvertise::getName, name)
                .eq(type != null, SmsHomeAdvertise::getType, type)
                .orderByDesc(SmsHomeAdvertise::getOrderCount);
        //3.进行分页查询
        smsHomeAdvertiseMapper.selectPage(page, wrapper);
        return page;
    }

    /**
     * 根据id进行查询
     *
     * @param id
     * @return
     */
    @Override
    public SmsHomeAdvertise findById(Integer id) {
        return smsHomeAdvertiseMapper.selectById(id);
    }

    /**
     * 进行修改
     *
     * @param smsHomeAdvertise
     */
    @Override
    public void update(SmsHomeAdvertise smsHomeAdvertise) {
        smsHomeAdvertiseMapper.updateById(smsHomeAdvertise);
    }
}
